package com.ad.pet.app.splash;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.ImageView;

import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.AppStatusManager;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.data.utils.SystemUitl;
import com.ad.pet.common.data.utils.glide.GlideUtil;
import com.ad.pet.common.data.utils.ui.DialogUtil;
import com.ad.pet.common.structure.key.TokenKey;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.BaseActivity;
import com.ad.pet.constant.AppStatusConstant;
import com.ad.pet.models.remote.home.BigAdver;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import rx.Observable;
import rx.functions.Action1;

/**
 * Created by SEELE on 2018/3/22.
 */

public class WelcomeActivity extends BaseActivity implements WelcomeContract.View {
    @Inject
    SpUtil mSpUtil;
    @Inject
    RxDisposable mRxDisposable;
    private RxPermissions mRxPermissions;
    private AlertDialog mDialog;

    private boolean mFlagVersion;

    @BindView(R.id.iv_open)
    ImageView mIvOpen;


    @Override
    protected void initViews() {

    }

    @Override
    protected void setContentViewBefore() {
        super.setContentViewBefore();
        AppStatusManager.getInstance().setAppStatus(AppStatusConstant.STATUS_NORMAL);
    }

    @Override
    protected void initData() {
        mRxPermissions = new RxPermissions(this);
        String versionName = mSpUtil.getString(TokenKey.VERSION);
        //是否第一次运行当前版本
        mFlagVersion = TextUtils.equals(versionName, SystemUitl.getVersionName(this));


        mDialog = DialogUtil.createDialogNoShow(mActivity, getString(R.string.tips), getString(R.string.refuse_read_storage), false, getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                }, getString(R.string.go_set), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDialog.dismiss();
                        Uri packageURI = Uri.parse("package:" + mActivity.getPackageName());
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                        mActivity.startActivity(intent);
                    }
                });

    }

    @Override
    protected void initEvents() {

    }

    @Override
    public void initInject() {
        DaggerSplachComponet.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build().inject(this);
    }

    private void next() {
        if (mFlagVersion) {
            Navigation.goMainActivity(mActivity);
        } else {
            Navigation.goSplashActivity(mActivity);
        }
        finish();
    }

    private void timer() {
        mRxDisposable.add(Observable.timer(3000, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        next();
                    }
                }));
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mFlagVersion) {
            DialogUtil.createPrivateDialog(mActivity, "温馨提示", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    autoEnter();

                }
            }).show();
        } else {
            autoEnter();
        }
    }

    private void autoEnter() {
        mRxDisposable.add(mRxPermissions.request(Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
//                if (aBoolean) {
                timer();
//                } else {
//                    mDialog.show();
//                }
            }
        }));
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        super.onDestroy();
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected boolean isStatusBarTransparent() {
        return true;
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getBootAdverSuccess(BigAdver bigAdver) {

        GlideUtil.loadNoDefault(mActivity, bigAdver.getPicLink(), mIvOpen);

    }
}
